package com.cxl.demo.service;

import com.cxl.demo.mapper.AssigmentMapper;
import com.cxl.demo.mapper.RecordGradeMapper;
import com.cxl.demo.mapper.RecordMapper;
import com.cxl.demo.mapper.UserMapper;
import com.cxl.demo.util.TimeUpd;
import com.cxl.demo.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RecordServiceImpl implements RecordService{

    @Autowired
    RecordMapper recordMapper;
    @Autowired
    AssigmentMapper assigmentMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RecordGradeMapper recordGradeMapper;


    @Override
    public Integer addRecord(Integer userId,Integer assigmentId){
        Assigment assigment=assigmentMapper.findAssigmentById(assigmentId);
        Integer recordNumber=recordMapper.recordNumberByAssigmentId(assigmentId);
        if(assigment==null||assigment.getNeedPeopleNumber()<=recordNumber){
            return 0;
        }




//        Date date= new Date();
//        String timestamp = String.valueOf(date.getTime());
        Record record=new Record();
        record.setGetRecordUserId(userId);
        record.setRecordGetTime(new TimeUpd().getDate());
        record.setRecordCondition(1);
        record.setAssigmentId(assigmentId);


        if(recordMapper.getIsRecord(record)!=0){
            return 0;
        }
        return recordMapper.addRecord(record);
    }

    @Override
    public Integer delRecord(Integer recordId) {
        Record record=recordMapper.getRecordByRecordId(recordId);
//        Date date= new Date();
        String timestamp =new TimeUpd().getDate();
        Assigment assigment=assigmentMapper.findAssigmentById(record.getAssigmentId());
        if(timestamp.compareTo(assigment.getEndTime())>=0||record.getRecordCondition()!=1){
            return 0;
        }
        return recordMapper.delRecord(recordId);
    }

    @Override
    public Integer updateRecordGrade(Record record) {
      //  Date date= new Date();
        String timestamp = new TimeUpd().getDate();
        recordMapper.updateRecord(record);
        record.setRecordGrade(null);
        //设置为已完成，设置完成时间
        record.setRecordCondition(3);

        record.setRecordEndTime(timestamp);
        recordMapper.updateRecord(record);
        //积分转交

        Record record1=recordMapper.getRecordByRecordId(record.getRecordId());
        Integer assigmentId=record1.getAssigmentId();
        //获取分成
        Double divide=recordGradeMapper.getGradeById(record1.getRecordGrade()).getRecordGradeDivide();
        Assigment assigment= assigmentMapper.findAssigmentById(assigmentId);
        //修改积分
        userMapper.updateAddScore(record1.getGetRecordUserId(),divide*assigment.getScore());

        return 1;
    }

    @Override
    public Integer updateRecordCondition(Record record) {

   //     Date date= new Date();
        String timestamp = new TimeUpd().getDate();
        //如果申请逾期扣积分
        if(record.getRecordCondition()==4){
            //通过recordId获取assigmentId进而进行时间比较
            Record record1=recordMapper.getRecordByRecordId(record.getRecordId());
            Integer assigmentId=record1.getAssigmentId();


            Assigment assigment= assigmentMapper.findAssigmentById(assigmentId);
            if(timestamp.compareTo(assigment.getEndTime())>0){
                recordMapper.updateRecord(record);
                userMapper.updateSubScore(record1.getGetRecordUserId(),2.0);
                return 1;
            }else{
                return 0;
            }
        }else{
            recordMapper.updateRecord(record);
            return 1;
        }
    }



    @Override
    public Pager AssigmentGetRecordList(Map<String,Object> map) {
        List<Record> list= recordMapper.getRecordsByAssigmentId(map);
        Pager pager=new Pager();
        pager.setRows(list);
        pager.setTotal(recordMapper.getRecordsByAssigmentIdNum((Integer) map.get("assigmentId")));
        return pager;
    }

    @Override
    public Pager UserGetRecordList(Map<String,Object> map) {

        List<Record> list= recordMapper.getRecordsByUserId(map);
        Pager pager=new Pager();
        pager.setRows(list);
        pager.setTotal(recordMapper.getRecordsByUserIdNum((Integer) map.get("userId")));
        return pager;

    }


    @Override
    public Object getChartVal(Integer userId) {

        Integer val=recordMapper.getRecordsByUserIdNum(userId);
        Map<String,Object> map=new HashMap<>();
        map.put("page",0);
        map.put("size",val);
        map.put("userId",userId);
        List<Record> list= recordMapper.getRecordsByUserId(map);
        Integer val1=0,val2=0,val3=0;
        Integer date1=0,date2=0,date3=0,date4=0,date5=0,date6=0,date7=0;
        for(Record item:list){

            if(item.getRecordGrade()==null){
                continue;
            }

            if(item.getRecordGrade()==1){
                val1++;
            }
            if(item.getRecordGrade()==2){
                val2++;
            }
            if(item.getRecordGrade()==3){
                val3++;
            }
        }
        Map<String,Object> mapPie=new HashMap<>();



       Map<String,Object> map1=new HashMap<>();
        Map<String,Object> map2=new HashMap<>();
        Map<String,Object> map3=new HashMap<>();
        map1.put("name","好评");
        map2.put("name","中评");
        map3.put("name","差评");
        map1.put("value",val1);
        map2.put("value",val2);
        map3.put("value",val3);


        List<Object> mapList=new ArrayList<>();
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);



       mapPie.put("valPie",mapList);

       map.put("size",assigmentMapper.getUserAssigmentNum(userId));
            List<Assigment> list3=assigmentMapper.findAssigmentByUserId(map);

            for(Assigment assigment:list3){

                switch (new TimeUpd().Data(assigment.getStartTime())){
                    case "星期一":
                        date1++;
                        break;
                    case "星期二":
                        date2++;
                        break;
                    case "星期三":
                        date3++;
                        break;
                    case "星期四":
                        date4++;
                        break;
                    case "星期五":
                        date5++;
                        break;
                    case "星期六":
                        date6++;
                        break;
                    case "星期日":
                        date7++;
                        break;

                }
            }
        List<String> list4=new ArrayList<>();
        list4.add("星期一");
        list4.add("星期二");
        list4.add("星期三");
        list4.add("星期四");
        list4.add("星期五");
        list4.add("星期六");
        list4.add("星期日");
        List<Integer> list5=new ArrayList<>();
        list5.add(date1);
        list5.add(date2);
        list5.add(date3);
        list5.add(date4);
        list5.add(date5);
        list5.add(date6);
        list5.add(date7);

        mapPie.put("txtZhu",list4);
        mapPie.put("valZhu",list5);



        return mapPie;
    }
}
